package com.ruoyi.well.service.impl;

import com.ruoyi.utils.MyDateUtil;
import com.ruoyi.well.domain.SerWellLog;
import com.ruoyi.well.domain.WellData;
import com.ruoyi.well.mapper.SerWellLogMapper;
import com.ruoyi.well.service.WellService;
import com.sun.xml.internal.ws.developer.Serialization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WellServiceImpl implements WellService {

    @Autowired
    private SerWellLogMapper wellLogMapper;

    @Override
    public List<WellData> waterDataStatisticsDay(String deviceIds, String year) {
        String startTime = year + "-01-01";
        String endTime = year + "-12-31";
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return null;
        }
        int unixStartTime = Math.toIntExact(MyDateUtil.date2UnixTime(startTime, "yyyy-MM-dd"));
        int unixEndTime = Math.toIntExact(MyDateUtil.date2UnixTime(endTime, "yyyy-MM-dd"));
//        List<SerWellLog> wellLogList = wellLogMapper.selectAll();
        List<SerWellLog>wellLogList = wellLogMapper.selectWellDataHistory(null,unixStartTime,unixEndTime);
        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));
        List<WellData> statistics = new ArrayList<>();
        for(String deviceId:deviceIdList) {
            for (String date : dates) {

                // 属性集合
                List<Double> waterVolList = new ArrayList<>();
//            List<Double> electricVolList = new ArrayList<>();
                WellData wellData = new WellData();
                wellData.setDate(date).setDeviceId(deviceId);

                for (SerWellLog wellLog : wellLogList) {
                    Date logDate = wellLog.getOffTime();
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(logDate);
                    int logYear = cal.get(Calendar.YEAR);
                    int logMonth = cal.get(Calendar.MONTH) + 1;
                    int logDay = cal.get(Calendar.DAY_OF_MONTH) + 1;
                    String logDayString = String.format("%04d-%02d-%02d", logYear, logMonth, logDay);
                    if (logDayString.equals(date)) {
                        waterVolList.add(wellLog.getWaterVol());
//                    electricVolList.add(wellLog.getElectricVol());
                    }
                }
                statistics.add(wellData);
            }
        }
        return statistics;
    }

    /**
     * 返回一年内每月的用水量
     *
     * @param deviceIds
     * @param year     需要统计的年份
     * @return
     */
    @Override
    public List<WellData> waterDataStatisticsMonth(String deviceIds, String year) {
        String startTime = year + "-01-01";
        String endTime = year + "-12-31";
        int unixStartTime = Math.toIntExact(MyDateUtil.date2UnixTime(startTime, "yyyy-MM-dd"));
        int unixEndTime = Math.toIntExact(MyDateUtil.date2UnixTime(endTime, "yyyy-MM-dd"));
//        List<SerWellLog> wellLogList = wellLogMapper.selectAll();
        List<SerWellLog> wellLogList = wellLogMapper.selectWellDataHistory(null, unixStartTime, unixEndTime);
        List<String> months = MyDateUtil.calculateMonthInterval(startTime, endTime);
        if (months == null) {
            return null;
        }
        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));

        List<WellData> statistics = new ArrayList<>();
        for (String month : months) {
            // 属性集合
            List<Double> waterVolList = new ArrayList<>();
//            List<Double> electricVolList = new ArrayList<>();
            WellData wellData = new WellData();
            wellData.setDate(month);
            for (SerWellLog wellLog : wellLogList) {
                String deviceId = wellLog.getDeviceId();
                Date logDate = wellLog.getOffTime();
                Calendar cal = Calendar.getInstance();
                cal.setTime(logDate);
                int logYear = cal.get(Calendar.YEAR);
                int logMonth = cal.get(Calendar.MONTH) + 1;
                String logMonthString = String.format("%04d-%02d", logYear, logMonth);
                if (logMonthString.equals(month) && deviceIdList.contains(deviceId)) {
                    waterVolList.add(wellLog.getWaterVol());
//                    electricVolList.add(wellLog.getElectricVol());
                }
            }
            wellData.setWaterVolMonth(waterVolList.parallelStream().mapToDouble(Double::doubleValue).sum());
//            wellData.setElectricVolMonth(electricVolList.parallelStream().mapToDouble(Double::doubleValue).sum());
            statistics.add(wellData);
        }
        return statistics;
    }
    /**
     * 根据设备返回一年内总用水量和用电量
     * @param deviceIds
     * @param startYear
     * @param endYear
     * @return
     */
    @Override
    public List<WellData> waterDataStatisticsYear(String deviceIds,String startYear,String endYear){
        String startTime = startYear + "-01-01";
        String endTime = endYear + "-12-31";
        int unixStartTime = Math.toIntExact(MyDateUtil.date2UnixTime(startTime, "yyyy-MM-dd"));
        int unixEndTime = Math.toIntExact(MyDateUtil.date2UnixTime(endTime, "yyyy-MM-dd"));
        List<SerWellLog> wellLogList = wellLogMapper.selectAll();
        List<String> years = MyDateUtil.calculateYearInterval(startYear,endYear);
        if (years == null) {
            return null;
        }

        String[] item = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(item));
        if(deviceIdList.isEmpty()){
            return null;
        }

        List<WellData> statistics = new ArrayList<>();
        for(String deviceId:deviceIdList) {
            for (String year : years) {
                // 属性集合
                List<Double> waterVolList = new ArrayList<>();
                List<Double> electricVolList = new ArrayList<>();
                WellData wellData = new WellData().setDeviceId(deviceId);
                wellData.setDate(year);
                for (SerWellLog wellLog : wellLogList) {
                    Date logDate = wellLog.getOffTime();
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(logDate);
                    int logYear = cal.get(Calendar.YEAR);
                    String logYearString = String.format("%04d", logYear);
                    if (logYearString.equals(year)) {
                        waterVolList.add(wellLog.getWaterVol());
                        electricVolList.add(wellLog.getElectricVol());
                    }
                }
                wellData.setWaterVolYear(waterVolList.parallelStream().mapToDouble(Double::doubleValue).sum());
                wellData.setElectricVolYear(electricVolList.parallelStream().mapToDouble(Double::doubleValue).sum());
                statistics.add(wellData);
            }
        }
        return statistics;
    }

    @Override
    public List<String> selectDeviceListByAreaId(Integer areaId){
        //取出的设备号需去重
        List<String> deviceIds = wellLogMapper.selectDeviceIdListByAreaId(areaId);
        Set<String> uniqueDeviceIds = new HashSet<>(deviceIds);
        return new ArrayList<>(uniqueDeviceIds);
    }

}
